﻿using System.Collections.Generic;

namespace Core
{
    public class Dict1
    {
        private abstract class Item
        {
            public object Value => InnerValue;
            protected virtual object InnerValue => Value;
        }

        private class Item<T> : Item
        {
            private T value;

            public Item(T value) => this.value = value;

            protected override object InnerValue => Value;

            public new T Value => value;

            public override string ToString()
            {
                return value.ToString();
            }
        }

        private readonly Dictionary<string, Item> dict = new();

        public void Add<T>(string key, T value)
        {
            dict.Add(key, new Item<T>(value));
        }

        public bool Remove(string key)
        {
            return dict.Remove(key);
        }

        public bool ContainsKey(string key)
        {
            return dict.ContainsKey(key);
        }

        public bool ContainsKey<T>(string key)
        {
            if (dict.ContainsKey(key) == false)
            {
                return false;
            }
            var shell = dict[key];
            var value = shell as Item<T>;
            if (value == null)
            {
                return false;
            }
            return true;
        }

        public T Find<T>(string key)
        {
            T value = default;
            if (ContainsKey<T>(key))
            {
                value = (dict[key] as Item<T>).Value;
            }
            return value;
        }

        public void Clear()
        {
            dict.Clear();
        }
    }
}
